
mixin getUserTimings(path, name, metric, type)
  - isSet = false
  if options.browsertime.iterations % 2 === 0 && type === 'median'
    td.number(data-title=type) #{metric[type]}
  else
    each run, index in path
      each marks in run.timings.userTimings.marks
          if (marks.name === name && Number(marks.startTime).toFixed(0) === Number(metric[type]).toFixed(0) && isSet === false)
            - isSet = true
            td.number(data-title=type)
              a(href='./' + (index+1) + '.html', title='Go to the run with the ' + type + ' ' + name) #{h.time.ms(metric[type])}

mixin getRow(name, object, path, runPath, metric, f)
  - const min = get(object, path + '.min')
  - const median = get(object, path + '.median')
  - const mean = get(object, path + '.mean')
  - const max = get(object, path + '.max')
  if min || min === 0
    tr
      td(data-title='metric') #{name}
        - let isSet = false
        each run, index in runPath
         if (Number(min).toFixed(0) === Number(get(run, metric)).toFixed(0) && isSet === false)
          - isSet = true
          td.number(data-title='min')
            a(href='./' + (index+1) + '.html', title='Go to the run with the min ' + name) #{f(min)}
        if options.browsertime.iterations % 2 === 0 
          td #{f(median)}
        else
          - isSet = false
          each run, index in runPath
            if (Number(median).toFixed(0) === Number(get(run, metric)).toFixed(0) && isSet === false)
              - isSet = true
              td.number(data-title='median')
                a(href='./' + (index+1) + '.html', title='Go to the run with the median ' + name) #{f(median)}
      td.number(data-title='mean') #{f(mean)}
      - isSet = false
      each run, index in runPath
         if (Number(max).toFixed(0) === Number(get(run, metric)).toFixed(0) && isSet === false)
          - isSet = true
          td.number(data-title='max')
            a(href='./' + (index+1) + '.html', title='Go to the run with the max ' + name) #{f(max)}
           
- btStatistics = h.get(pageInfo.data, 'browsertime.pageSummary.statistics')

if btStatistics
  a#browsertime-timing-statistics
  h2 Timings Summary
  - timingMetrics = ['firstPaint', 'loadEventEnd']
  .responsive
    table
      thead
        tr
          th Metric
          th.number min
          th.number median
          th.number mean
          th.number max
      if btStatistics.visualMetrics
        tr 
          td.extraheader(colspan='5') Visual Metrics
        each visualMetric, name in btStatistics.visualMetrics
          if name !== 'videoRecordingStart'
            tr
              +getRow(name, pageInfo, 'data.browsertime.pageSummary.statistics.visualMetrics.' + name, pageInfo.data.browsertime.pageSummary.visualMetrics, name, h.time.ms)
      if btStatistics.timings && btStatistics.timings.pageTimings
        tr 
          td.extraheader(colspan='5') Google Web Vitals
          +getRow('Time To First Byte (TTFB)', pageInfo, 'data.browsertime.pageSummary.statistics.timings.pageTimings.backEndTime' , pageInfo.data.browsertime.pageSummary.browserScripts, 'timings.pageTimings.backEndTime', h.time.ms)
          +getRow('Largest Contentful Paint (LCP)', pageInfo, 'data.browsertime.pageSummary.statistics.timings.largestContentfulPaint.renderTime' , pageInfo.data.browsertime.pageSummary.browserScripts, 'timings.largestContentfulPaint.renderTime', h.time.ms)
          +getRow('First Contentful Paint (FCP)', pageInfo, 'data.browsertime.pageSummary.statistics.timings.paintTiming["first-contentful-paint"]', pageInfo.data.browsertime.pageSummary.browserScripts, 'timings.paintTiming["first-contentful-paint"]', h.time.ms)
          +getRow('Cumulative Layout Shift (CLS)', pageInfo, 'data.browsertime.pageSummary.statistics.pageinfo.cumulativeLayoutShift' , pageInfo.data.browsertime.pageSummary.browserScripts, 'pageinfo.cumulativeLayoutShift', h.noop)
        tr 
          td.extraheader(colspan='5') More metrics
        each timing in timingMetrics
          +getRow(timing, pageInfo, 'data.browsertime.pageSummary.statistics.timings.' + timing , pageInfo.data.browsertime.pageSummary.browserScripts, 'timings.' + timing, h.time.ms)
         
      if btStatistics.timings && btStatistics.timings.userTimings && btStatistics.timings.userTimings.marks
        tr 
          td.extraheader(colspan='5') User Timing 
        each userTimings, name in btStatistics.timings.userTimings.marks
          tr
            td(data-title= 'User Timing') #{name}
            +getUserTimings(pageInfo.data.browsertime.pageSummary.browserScripts, name, userTimings, 'min')
            +getUserTimings(pageInfo.data.browsertime.pageSummary.browserScripts, name, userTimings, 'median')
            td.number(data-title='mean') #{userTimings ? h.time.ms(userTimings.mean): ''}
            +getUserTimings(pageInfo.data.browsertime.pageSummary.browserScripts, name, userTimings, 'max')
      if btStatistics.cpu && btStatistics.cpu.longTasks
        tr 
          td.extraheader(colspan='5') CPU
        +getRow('Total Blocking Time', pageInfo, 'data.browsertime.pageSummary.statistics.cpu.longTasks.totalBlockingTime' , pageInfo.data.browsertime.pageSummary.cpu, 'longTasks.totalBlockingTime', h.time.ms)
        +getRow('Max Potential FID', pageInfo, 'data.browsertime.pageSummary.statistics.cpu.longTasks.maxPotentialFid', pageInfo.data.browsertime.pageSummary.cpu, 'longTasks.maxPotentialFid', h.time.ms)
        +getRow('CPU long tasks ', pageInfo, 'data.browsertime.pageSummary.statistics.cpu.longTasks.tasks' , pageInfo.data.browsertime.pageSummary.cpu, 'longTasks.tasks', h.noop)
        +getRow('CPU longest task duration ', pageInfo, 'data.browsertime.pageSummary.statistics.cpu.longTasks.durations.max' , pageInfo.data.browsertime.pageSummary.cpu, 'longTasks.durations.max', h.time.ms)
        +getRow('CPU last long task happens at', pageInfo, 'data.browsertime.pageSummary.statistics.cpu.longTasks.lastLongTask' , pageInfo.data.browsertime.pageSummary.cpu, 'longTasks.lastLongTask', h.time.ms)
